<?php
/**
 * Created by PhpStorm.
 * User: rocky
 * Date: 2019-07-14
 * Time: 01:47
 */

namespace app\common\resource;


use League\Fractal\Manager;

class BaseResource
{
    //集合数据转换层
    public static function collection($data)
    {
        $self = new static();
        $resource = new \League\Fractal\Resource\Collection($data, [$self, 'toArray']);
        $fractal = new Manager();
        $data = $fractal->createData($resource)->toArray();
        return $data['data'];
    }
    //数据转换层
    public static function item($data)
    {
        $self = new static();
        $resource = new \League\Fractal\Resource\Item($data, [$self, 'toArray']);
        $fractal = new Manager();
        $data = $fractal->createData($resource)->toArray();
        return $data['data'];
    }
}